package hmis.pos.Service.ServiceImpl;

import hmis.pos.Base.BaseService.Impl.BaseServiceImpl;
import hmis.pos.Entity.Order;
import hmis.pos.Entity.OrderDetail;
import hmis.pos.Repository.OrderDetailRepository;
import hmis.pos.Service.OrderDetailService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class OrderDetailServiceIplm extends BaseServiceImpl<OrderDetail,OrderDetailRepository> implements OrderDetailService {

    @Autowired
    private OrderDetailRepository orderDetailRepository;


    /*@Override
    public OrderDetail sava(OrderDetailAndDate orderDetailAndDate) {
        OrderDetail orderDetail = orderDetailAndDate.getOrderDetail();
        Date in_time = orderDetailAndDate.getIn_time();
        Date out_time = orderDetailAndDate.getOut_time();
        String type = orderDetail.getType();
        TypeAndDate typeAndDate = new TypeAndDate();
        typeAndDate.setType(type);
        typeAndDate.setIn_time(in_time);
        typeAndDate.setOut_time(out_time);
        double surtotal = 0;

        List<RoomPrice> roomPriceList = roomPriceService.findByTypeAndDateBetween(typeAndDate);
        int minSurplus = 0;
        if(roomPriceList.size()!=0){
            minSurplus = roomPriceList.get(0).getSurplus();
        }else{ return null; }

        for(RoomPrice roomPrice : roomPriceList){
            if(roomPrice.getSurplus()<minSurplus)
                minSurplus = roomPrice.getSurplus();
        }
        if(minSurplus==0){return null ;};

        for(RoomPrice roomPrice : roomPriceList){
            roomPrice.setSurplus(roomPrice.getSurplus()-minSurplus);
            surtotal += roomPrice.getPrice();
            roomPriceService.update(roomPrice);
        }

        surtotal *= orderDetail.getRoomnumber();
        if(surtotal==0)return null;
        orderDetail.setSubtotal(surtotal);

        return orderDetailRepository.save(orderDetail);

    }*/

    @Override
    public List<OrderDetail> findByOrder(Order order) {
        return orderDetailRepository.findByOrderId(order.getId());
    }

    @Override
    public boolean deleteByOrderid(String orderid) {
        if(orderDetailRepository.findByOrderId(orderid).size()==0)
            return false;
        orderDetailRepository.deleteByOrderid(orderid);
        return true;
    }


}
